<template>
  <div class="app-container cluster">
    <div class="cluster-header">
      <el-button type="primary" size="mini" @click="clickCreateCluster">新建集群</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      fit
      highlight-current-row>
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="集群名称">
        <template slot-scope="scope">
          <a class="table-link" @click="toDetail(scope.row)">{{ scope.row.name }}</a>
        </template>
      </el-table-column>
      <el-table-column label="IP" width="200" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.ip }}</span>
        </template>
      </el-table-column>
      <el-table-column label="URI" width="200" align="center">
        <template slot-scope="scope">
          {{ scope.row.uri }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <a class="table-action" @click="clickDeleteCluster(scope.row)">删除</a>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :visible.sync="dialog.createCluster"
      title="新建集群"
      width="500px"
      @close="resetForm('createClusterForm')">
      <el-form ref="createClusterForm" :model="createClusterForm" :rules="rulesCreateCluster" label-width="100px">
        <el-form-item label="集群名称：" prop="name">
          <el-input v-model="createClusterForm.name" />
        </el-form-item>
        <el-form-item label="IP：" prop="ip">
          <el-input v-model="createClusterForm.ip" />
        </el-form-item>
        <el-form-item label="端口：" prop="port">
          <el-input v-model="createClusterForm.port" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog.createCluster = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('createClusterForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getClusterList, createCluster, deleteCluster } from '@/api/cluster'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      dialog: {
        createCluster: false
      },
      createClusterForm: {
        name: '',
        ip: '',
        port: ''
      },
      rulesCreateCluster: {
        name: [
          { required: true, message: '请输入集群名称', trigger: 'blur' }
        ],
        ip: [
          { required: true, message: '请输入节点IP', trigger: 'blur' }
        ],
        port: [
          { required: true, message: '请输入端口', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this._fetchData()
  },
  methods: {
    // 点击新建集群
    clickCreateCluster() {
      this.dialog.createCluster = true
    },
    // 点击删除集群
    clickDeleteCluster(row) {
      this.$confirm('此操作将删除该集群, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this._deleteCluster(row)
      })
    },
    // 点击table名称
    toDetail(row) {
      this.$store.dispatch('SaveCurCLuster', row)
      this.$router.push({ path: '/cluster/nodes' })
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 表单提交(createClusterForm: 新建集群)
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this._createCluster()
          this.dialog.createCluster = false
          this.resetForm(formName)
        } else {
          return false
        }
      })
    },
    // 查询集群列表
    _fetchData() {
      this.listLoading = true
      getClusterList().then(res => {
        if (res.message.code === 0) {
          this.listLoading = false
          this.list = res.data.clusters_info
        } else {
          this.$message({ message: res.message.message, showClose: true, type: 'error' })
        }
      })
    },
    // 创建集群
    _createCluster() {
      const data = {
        'cluster_name': this.createClusterForm.name,
        'k8s_master_ip': this.createClusterForm.ip,
        'k8s_master_port': this.createClusterForm.port
      }
      createCluster(data).then(res => {
        if (res.message.code === 0) {
          this.$message({ message: '创建集群成功', showClose: true, type: 'success' })
          this._fetchData()
        } else {
          this.$message({ message: res.message.message, showClose: true, type: 'error' })
        }
      })
    },
    // 删除集群
    _deleteCluster(row) {
      const param = row.name
      deleteCluster(param).then(res => {
        if (res.message.code === 0) {
          this._fetchData()
          this.$message({ message: '集群删除成功', showClose: true, type: 'success' })
        } else {
          this.$message({ message: res.message.message, showClose: true, type: 'error' })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.cluster {
  &-header {
    padding-bottom: 10px;
  }
  label {
    font-weight: normal
  }
  .table-action {
    color: #4285f4;
  }
  .table-link {
    color: #4285f4;
  }
}
</style>
